package com.jy.wms.web;

import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

@Controller
@RequestMapping("/import")
public class ImportModuleController {
    @RequestMapping(value = "/toImportPage")
    public String toUser(){
        return "import/importPage";
    }
    /**
     * 上传文件
     * @param files
     * @param request
     * @param response
     * @param fpath
     * @return
     */
    @RequestMapping(value = "/importFile")
    @ResponseBody
    public String uploadShare(@RequestParam("file") MultipartFile[] files,
                              HttpServletRequest request, HttpServletResponse response, String fpath, String standardName){
        if(fpath==null||"/".equals(fpath)){
            fpath="/";
        }
        String realPath = request.getSession().getServletContext().getRealPath("/");
        String filePath = realPath + "WEB-INF"+File.separator + "classes" + File.separator +"static" + File.separator + "resource" + File.separator;

        System.out.println(filePath);
        File filefolder=new File(filePath);
        if(!filefolder.isDirectory()){
            filefolder.mkdirs();
        }
        for(MultipartFile file:files){
            File fe=new File(filePath+"/"+file.getOriginalFilename());
            try {
                file.transferTo(fe);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        JSONObject json=new JSONObject();
        String projectPath = request.getSession().getServletContext().getContextPath();
        String contextpath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+projectPath;
        String url = contextpath + "/resource/"+files[0].getOriginalFilename();
        System.out.println(url);
        json.accumulate("flag", true);
        return json.toString();
    }
}
